package com.liuweidihuangwan.jingdong.controller;


import com.liuweidihuangwan.jingdong.common.BaseResponse;
import com.liuweidihuangwan.jingdong.common.ResultUtils;
import com.liuweidihuangwan.jingdong.exception.ThrowUtils;
import com.liuweidihuangwan.jingdong.model.dto.address.AddressAddRequest;
import com.liuweidihuangwan.jingdong.model.dto.address.AddressDefaultRequest;
import com.liuweidihuangwan.jingdong.model.dto.address.AddressEditRequest;
import com.liuweidihuangwan.jingdong.model.vo.AddressVO;
import com.liuweidihuangwan.jingdong.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-08
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址相关接口")
public class AddressController {

    @Resource
    AddressService addressService;

    /**
     * 新增地址
     * @param addressAddRequest
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增地址")
    public BaseResponse<Integer> addAddress(@RequestBody AddressAddRequest addressAddRequest){
        Integer addressId = addressService.addAddress(addressAddRequest);
        return ResultUtils.success(addressId);
    }

    /**
     * 查询用户地址
     * @return
     */
    @GetMapping("/query")
    @ApiOperation("查询用户地址")
    public BaseResponse<List<AddressVO>> queryAddress(){
        List<AddressVO> addressVOS = addressService.queryAddressList();
        return ResultUtils.success(addressVOS);
    }

    /**
     * 编辑地址
     * @param addressEditRequest
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation("编辑地址")
    public BaseResponse<Integer> editAddress(@RequestBody AddressEditRequest addressEditRequest){
        Integer addressId = addressService.editAddress(addressEditRequest);
        return ResultUtils.success(addressId);
    }

    /**
     * 删除地址
     * @param addressId
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除数据")
    public BaseResponse<Integer> deleteAddress(@RequestParam(value = "addressId",required = false) Integer addressId){
        Integer id = addressService.deleteAddress(addressId);
        return ResultUtils.success(id);
    }

    @PutMapping("/default")
    @ApiOperation("地址设置默认接口")
    public BaseResponse<Integer> defaultAddress(@RequestBody AddressDefaultRequest addressDefaultRequest){
        Integer addressId = addressService.setDefaultAddress(addressDefaultRequest);
        return ResultUtils.success(addressId);
    }
}
